<?php
	if ( ! defined('BASEPATH')) exit('No direct script access allowed');

	class MY_Controller extends CI_Controller{

	public $user_data = array();
	public $logged_in = 0;
	public $school_id;
	public $jump_url;
	public $offset = 10;
		function __construct() {
			parent::__construct();
			$this->jump_url = !empty($_SERVER['HTTP_REFERER']) ? $_SERVER['HTTP_REFERER'] : '/index';
			$curr_url = 'http://'.$_SERVER['HTTP_HOST'].str_replace('/index.php','',$_SERVER['PHP_SELF']);
			// echo str_replace('/index.php','',$_SERVER['PHP_SELF']);
			if(empty($_SERVER['QUERY_STRING'])==false){
				$curr_url = $curr_url.'?'.$_SERVER['QUERY_STRING'];
			}

			if(strstr($_SERVER['PHP_SELF'],"logreg")==false&&strstr($_SERVER['PHP_SELF'],"login")==false&&strstr($_SERVER['PHP_SELF'],"png")==false&&strstr($_SERVER['PHP_SELF'],"jpg")==false&&strstr($_SERVER['PHP_SELF'],"reg")==false&&strstr($_SERVER['PHP_SELF'],"video_comment_list")==false){
				$this->session->set_userdata('curr_url', $curr_url);
			}
			$this->load->library('session');
			$this->user_data = $this->session->userdata('user_data');
			$this->load->model('User_model');
			if(empty($this->user_data)){
				$this->logged_in = 0;
			}else{
				$user = $this->User_model->get_user_by_id($this->user_data['id']);
				if(empty($user))
				{
				  $this->session->sess_destroy();
					unset(
						$_SESSION['id'],
						$_SESSION['head_image'],
						$_SESSION['user_name'],
						$_SESSION['mobile'],
						$_SESSION['logged_in']
					);
					$this->logged_in = 0;
					exit;
				}else{
					$this->smarty->assign("user", $user);
				}
				$this->logged_in = 1;

				$this->smarty->assign("user_data", $this->user_data);
			}
			$this->smarty->assign("logged_in", $this->logged_in);
			/*global $app_name;
			if($app_name != ''){*/
				$school_info = $this->User_model->get_school();
				if($school_info){

					$start_time = strtotime($school_info['start_date']);
					$end_time = strtotime($school_info['end_date']);
					$today_time = strtotime(date("Y-m-d"));
					if($start_time > $today_time || $end_time < $today_time){
						show_error("该网校已经停止运营");exit;
					}

					$this->school_id = $school_info['id'];
					$menu_list = $this->school_menus();
					$menu_list_data = array();
					if(count($menu_list) > 0){
						foreach ($menu_list as $key => $val) {
							if($val['pid'] == 0){
								$menu_list_data[$val['id']]['info'] = $val;
							}else{
								$menu_list_data[$val['pid']]['sub'][] = $val;
							}
						}
					}

					$page_list = $this->school_page();
					$this->load->model("Helps");
					$this->load->model("Friend_links");
					$this->load->model("Newses");
					$this->load->model("Questions");
					$this->load->model("Wangdians");
					$this->load->model('Videos');
					$this->load->model("Question_categorys");

					$conditions = array('school_id' => $this->school_id);
					$help_category_list =  $this->Helps->get_help_category_list(0,0,$conditions);
					$index_friend_links = $this->Friend_links->get_friend_link_list(0, 0, array("school_id" => $this->school_id));
					$news_list_data = $this->Newses->get_category();
					$course_cate = $this->Videos->get_video_cate();

					$category_list = $this->Question_categorys->get_top_categorys();
					$conditions = array('category_id' => 0, "school_id" => $this->school_id, "type" => 1);
					$index_questions = $this->Questions->get_question_list(0, 4, $conditions);
					$index_wangdian = $this->Wangdians->get_wangdian_list(0, 20, array("school_id"=>$this->school_id));
					
					$this->smarty->assign("index_questions", $index_questions);
					$this->smarty->assign("index_friend_links", $index_friend_links);
					$this->smarty->assign("help_category_list", $help_category_list);
        			$this->smarty->assign("news_list_data", $news_list_data);
					$this->smarty->assign("course_cate", $course_cate);
					$this->smarty->assign("menu_list", $menu_list_data);
					$this->smarty->assign("category_list", $category_list);
					$this->smarty->assign("index_wangdian", $index_wangdian);

					$this->smarty->assign("school_info", $school_info);
					$this->smarty->assign("page_list", $page_list['result']);
					$this->smarty->assign("jump_url", $this->jump_url);
				}else{
					show_error("Error:您访问的站点不存在");exit;
				}
				
			/*}else{
				echo 'Error:domain prefix is null';
			}*/
		}

		function school_menus(){
			$this->load->model("School_menus");
			$menu_list = $this->School_menus->get_school_menus($this->school_id);
			return $menu_list;
		}

		function school_page(){
			$this->load->model("School_pages");
			$conditions['school_id'] = $this->school_id;
			$conditions['show_menu'] = 1;
			$page_list = $this->School_pages->get_list(0, 0, $conditions);
			return $page_list;
		}

		function get_school_info(){
			$this->load->model("School_model");
			$conditions['school_id'] = $this->school_id;
			$school_info = $this->School_model->get_school_by_id($this->school_id);
			return $school_info;
		}
	}

  class User_Controller extends MY_Controller{

	public $user_data = array();
	public $logged_in = 0;

	function __construct() {
		parent::__construct();

		$curr_url = 'http://'.$_SERVER['HTTP_HOST'].str_replace('/index.php','',$_SERVER['PHP_SELF']);
		// echo str_replace('/index.php','',$_SERVER['PHP_SELF']);
		if(empty($_SERVER['QUERY_STRING'])==false){
			$curr_url = $curr_url.'?'.$_SERVER['QUERY_STRING'];
		}

		if(strstr($_SERVER['PHP_SELF'],"login")==false)
			$this->session->set_userdata('curr_url', $curr_url);

		$this->load->library('session');
		$this->user_data = $this->session->userdata('user_data');

		if(empty($this->user_data)){
//            print_r("null");
			// redirect('/login');exit;
			//用户存在
			header("Location:"."http://{$_SERVER['HTTP_HOST']}/login");
			exit;
		}else{
			$this->load->model('User_model');
			$user = $this->User_model->get_user_by_id($this->user_data['id']);
			if(empty($user))
			{
				unset(
					$_SESSION['id'],
					$_SESSION['head_image'],
					$_SESSION['user_name'],
					$_SESSION['mobile'],
					$_SESSION['logged_in']
				);
				header("Location:"."http://".$_SERVER['HTTP_HOST']."/login");
				exit;
			}
			$this->logged_in = 1;
			$this->smarty->assign("logged_in", $this->logged_in);
			$this->smarty->assign("user_data", $this->user_data);
		}


	}
	}
